package com.base.llpark;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.RxBus2;
import com.base.llpark.adapter.BaseAdapter;
import com.base.llpark.adapter.BaseHolder;
import com.base.llpark.bean.IllegalPark;
import com.base.llpark.bean.ParkDetail;
import com.base.llpark.databinding.ActivityParklistBinding;
import com.base.llpark.presenter.ParkHistoryListPresenter;
import com.base.llpark.utills.GlideUtil;
import com.base.llpark.view.ParkHistoryListView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by weikailiang on 2019/10/26.
 */
@Route(path = "/history/parklist")
public class ParkHistoryListActivity extends CBaseBarMvpActivity<ParkHistoryListPresenter> implements ParkHistoryListView{
    private ActivityParklistBinding mBinding;
    private List<IllegalPark> mData=new ArrayList<>();
    private BaseAdapter<IllegalPark> mAdapter;
    private int page = 1;
    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityParklistBinding) mRootBinding;
        mPresenter = new ParkHistoryListPresenter(this,this);
        mPresenter.start();
        setBackNavigation();
        setBarTitle("违停历史");
        mBinding.rl.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.rl.setHeaderHeight(60);
        mBinding.rl.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_parklist;
    }

    @Override
    protected void start() {
        mAdapter = new BaseAdapter<IllegalPark>(mData, mContext) {

            @Override
            public void convert(BaseHolder holder, IllegalPark park, boolean isHeader, boolean isBottom) {
                ImageView iv = holder.getView(R.id.iv);
                TextView num = holder.getView(R.id.num);
                TextView address = holder.getView(R.id.address);
                TextView date = holder.getView(R.id.date);
                ImageView iv_state = holder.getView(R.id.state);
                if (park.getStateName() != null && park.getStateName().equals("未处理")) {
                    GlideUtil.loadResource(mContext, R.drawable.wcl, iv_state);

                } else if (park.getStateName() != null && park.getStateName().equals("罚款处理") || park.getStateName() != null && park.getStateName().equals("销案处理") || park.getStateName() != null && park.getStateName().equals("教育处理")) {
                    GlideUtil.loadResource(mContext, R.drawable.ycl, iv_state);

                } else if (park.getStateName() != null && park.getStateName().equals("移交公安")) {
                    GlideUtil.loadResource(mContext, R.drawable.yyj, iv_state);
                } else if (park.getStateName() != null && park.getStateName().equals("转入处罚")) {
                    GlideUtil.loadResource(mContext, R.drawable.zhuruchufa, iv_state);
                }else {
                    //未上报
                }
                GlideUtil.load(mContext, RealNetWorkUtil.getBaseUrl()+"/api-file/"+park.getFileAddress(), iv);
                num.setText(park.getCarNo());
                address.setText(park.getCheckSignAddressName());
                date.setText(park.getCheckDate());

            }
        };
        mAdapter.putNormalType(R.layout.item_park);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                IllegalPark illegalPark = mData.get(dataPosition);
                ARouter.getInstance().build("/lpark/parkdetail").withParcelable("mPark",illegalPark).withInt("mType",1).navigation();
            }
        });
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rv.setAdapter(mAdapter);
        mBinding.rl.autoRefresh();

        mBinding.rl.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page ++;
                mPresenter.getHistoryList(String.valueOf(page));
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getHistoryList(String.valueOf(page));
            }
        });

        Disposable disposable = RxBus2.getInstance().doSubscribe(ParkDetail.class, new Consumer<ParkDetail>() {
            @Override
            public void accept(ParkDetail randomRefreshEvent) throws Exception {
                page = 1;
                mPresenter.getHistoryList(String.valueOf(page));
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);

    }


    @Override
    public void returnNoData() {
        mBinding.rl.finishRefresh();
        mBinding.empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnList(List<IllegalPark> list) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.finishRefresh();
        mBinding.rl.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(List<IllegalPark> list) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.finishLoadmore();
        mBinding.rl.setLoadmoreFinished(false);
        mData.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.finishLoadmore();
        mBinding.rl.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
        if (page ==1){
            mBinding.rl.finishRefresh();
        }else {
            mBinding.rl.finishLoadmore();
        }
    }
}
